package com.bs.sc.mgr.order.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bs.sc.base.utils.AjaxJson;
import com.bs.sc.mgr.order.entity.Order;
import com.bs.sc.mgr.order.service.OrderService;

@RestController
@RequestMapping(value = "/order")
@CrossOrigin
public class OrderController {
	@Autowired
	private OrderService orderService;

	/**
	 * 创建订单
	 * 
	 * @param request
	 * @param order
	 * @param goodsIds
	 * @return
	 */
	@RequestMapping(value = "createOrder", method = RequestMethod.POST)
	public AjaxJson createOrder(HttpServletRequest request, Order order, String goodsIds) {
		if (!StringUtils.isEmpty(goodsIds)) {
			return orderService.createOrder(request, order, goodsIds.split(","));
		} else {
			AjaxJson j = new AjaxJson();
			j.setCode("failure");
			j.setMsg("订单商品不能为空");
			return j;
		}
	}

	/**
	 * 分页查询订单信息
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "findOrderList", method = RequestMethod.POST)
	public AjaxJson findOrderList(int pageNumber, int pageSize) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取分页数据失败");
		try {
			Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
			Page<Order> page = orderService.findOrderList(pageable);
			Map<String, Object> pagaData = new HashMap<>();
			pagaData.put("orderList", page.getContent());
			pagaData.put("count", page.getTotalElements());
			j.setData(pagaData);
			j.setCode("success");
			j.setMsg("获取分页数据成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	/**
	 * 获取订单信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getOrder", method = RequestMethod.GET)
	public AjaxJson getOrder(String id) {
		return orderService.getOrder(id);
	}

	/**
	 * 获取我的订单
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getMyOrder", method = RequestMethod.POST)
	public AjaxJson getMyOrder(HttpServletRequest request) {
		return orderService.getMyOrder(request);
	}

	/**
	 * 评分
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "updateItemGrade", method = RequestMethod.POST)
	public AjaxJson updateItemGrade(String itemId, int grade, String comment) {
		return orderService.updateItemGrade(itemId, grade, comment);
	}

	/**
	 * 删除订单中商品
	 * 
	 * @param itemId
	 * @return
	 */
	@RequestMapping(value = "deleteItemGrade", method = RequestMethod.POST)
	public AjaxJson deleteItemGrade(String itemId) {
		return orderService.deleteItemGrade(itemId);
	}
}
